package com.wannengbang.cloudleader.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.utils.Dp2Px;
import com.wannengbang.cloudleader.utils.ScreenUtils;

import java.sql.Array;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Author:HWC
 */
public class LuckDrawDialog extends DialogFragment {

    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.iv_dismiss)
    ImageView iv_dismiss;
    private Dialog dialog;
    Unbinder unbinder;
    private Context mContext;
    private static String mmoney;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_luck_draw, null);
        unbinder = ButterKnife.bind(this, view);
        mContext = getActivity();
        initView();
        return view;
    }

    public static LuckDrawDialog newInstance(String money) {
        mmoney = money;
        LuckDrawDialog fragment = new LuckDrawDialog();
        return fragment;
    }

    //    初始化请求
    private void initView() {
        tv_money.setText(mmoney+"元");
        iv_dismiss.setOnClickListener(v -> {
            dismiss();
        });
    }


    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return true;
            }
        });
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (ScreenUtils.getScreenWidth(mContext) - Dp2Px.dp2Px(40));
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
